var _GADone = true;
var _GAList = new Array();
var _GASpriteList = new Array();
var _GACounter = 0;
var _GADestoryCache;

function GameAnm(pic,no,target){
	this.pic = pic;
	this.no = no;
	this.target = target;
};

function GAPlayAction(){
	_GLState = "动画运行";
	_GADone = false;
	GAMultiTarget();
};

function TargetToMulti(target){
	var temp = new Array();
	temp.push(target);
	return temp;
};

function GAAnmCounter(){
	_GACounter -- ;
	if(_GACounter<=0){
		for(var i in _GASpriteList){
			_GASpriteList[i].removeFromParent(true);
		}
		_GASpriteList = new Array();
		_GADone = true;
	}
};

function GADestoryCounter(){
	_GACounter -- ;
	if(_GACounter<=0){
		CSDestory(_GADestoryCache);
		_GADone = true;
	}
};

function GAnextAnm(){
	_GADone = true;
};

// 棋子移动
function GAChessMove(id,from,to){
	_GLState = "动画运行";
	_GADone = false;
	_GACounter = 1;
	_board[to.x][to.y] = _board[from.x][from.y];
	_board[to.x][to.y].position = new GDposition(to.x,to.y);
	_board[from.x][from.y] = null;
	_GBAction = new cc.MoveTo(0.6, cc.p(CChessPositionx(to.x), CChessPositiony(to.y)));
	var act = cc.Sequence.create(_GBAction,cc.CallFunc.create(function(){GAAnmCounter()}));
	_board[to.x][to.y].sprite.runAction(act);
};

function GAriseChess(){
	var target = CFindById(_GAList[0].target);
	target.sprite.setLocalZOrder(50);
	_GACounter = 1;
	var act = cc.Sequence.create(new cc.ScaleBy(0.6,1.2),cc.CallFunc.create(function(){GAAnmCounter()}));
	_GAList.shift();
	target.sprite.runAction(act);
};

function GAdownChess(){
	var target = CFindById(_GAList[0].target);
	target.sprite.setLocalZOrder(0);
	_GACounter = 0;
	for(var i in _GAList[0].target){
		var act = cc.Sequence.create(new cc.ScaleBy(0.6,1/1.2),cc.CallFunc.create(function(){GAAnmCounter()}));
		_GACounter++;
		CFindById(_GAList[0].target[i]).sprite.runAction(act);
	}
	_GAList.shift();
};

function GAChessDestory(){
	_GADestoryCache = _GAList[0].target[0];
	_GACounter = 0;
	for(var i in _GAList[0].target){
		var act = cc.Sequence.create(new cc.FadeOut(0.6),cc.CallFunc.create(function(){GADestoryCounter()}));
		_GACounter ++;
		CFindById(_GAList[0].target[0]).sprite.runAction(act);
	}
	_GAList.shift();
};

function GAShake(){
	_GACounter = 0;
	for(var i in _GAList[0].target){
		_GACounter++;
		var moveList = new Array();
		var position = new GDposition(CFindById(_GAList[0].target[i]).sprite.getPositionX(), CFindById(_GAList[0].target[i]).sprite.getPositionY());
		for(var j=0;j<7;j++){
			var action;
			if(j%2 == 0)
				action = new cc.MoveTo(0.05, cc.p(position.x-10, position.y));
			else
				action = new cc.MoveTo(0.05, cc.p(position.x+10, position.y));
			moveList.push(action);
		}
		var action = new cc.MoveTo(0.1, cc.p(position.x, position.y));
		moveList.push(action);
		var act = cc.Sequence.create(moveList[0],moveList[1],moveList[2],moveList[3],moveList[4],moveList[5],moveList[6],action,cc.CallFunc.create(function(){GAAnmCounter()}));
		CFindById(_GAList[0].target[i]).sprite.runAction(act);
	}
	_GAList.shift();
};

function GAMultiTarget(){
	if(_GAList[0].pic == "抬起"){
		GAriseChess();
		return;
	}
	if(_GAList[0].pic == "恢复"){
		GAdownChess();
		return;
	}
	if(_GAList[0].pic == "破坏"){
		GAChessDestory();
		return;
	}
	if(_GAList[0].pic == "震动"){
		GAShake();
		return;
	}
	_GACounter = 0;
	for(var i in _GAList[0].target){
		var animation =new cc.Animation();
		animation.addSpriteFrame(new cc.SpriteFrame(_GAList[0].pic, cc.rect(0, _GAList[0].no*100, 100, 100)));
		animation.addSpriteFrame(new cc.SpriteFrame(_GAList[0].pic, cc.rect(100, _GAList[0].no*100, 100, 100)));
		animation.addSpriteFrame(new cc.SpriteFrame(_GAList[0].pic, cc.rect(200, _GAList[0].no*100, 100, 100)));
		animation.addSpriteFrame(new cc.SpriteFrame(_GAList[0].pic, cc.rect(300, _GAList[0].no*100, 100, 100)));
		animation.addSpriteFrame(new cc.SpriteFrame(_GAList[0].pic, cc.rect(400, _GAList[0].no*100, 100, 100)));
		animation.setDelayPerUnit(0.15);
		var act = cc.Sequence.create(cc.animate(animation),cc.CallFunc.create(function(){GAAnmCounter()},this));
		_GACounter++;
		_GASpriteList.push(new cc.Sprite());
		_GASpriteList[i].setPosition(CChessPositionx(_GAList[0].target[i].x),CChessPositiony(_GAList[0].target[i].y));
		_GASpriteList[i].setLocalZOrder(100);
		g_GBTouchLayer.addChild(_GASpriteList[i]);
		_GASpriteList[i].runAction(act);
	}
	_GAList.shift();
};

function GAShowNowTurn(){
	var showTurn  = new cc.Sprite(res.UiModule_png,cc.rect(0, 0, 640, 200));
	showTurn.attr({
		anchorX : 0,
		anchorY : 0.5,
		x: 640,
		y: 650
	});
	var portraitUp = new cc.Sprite("res/head_speech.png",cc.rect(450, 0, 150, 150));
	portraitUp.attr({
		anchorX : 0,
		anchorY : 0.5,
		x: 35,
		y: 110
	});
	showTurn.addChild(portraitUp,900);
	var textBoxUp = new cc.LabelTTF("第"+_GLTurn+"回合开始","res/font/Microsoft Yahei.ttf", 24);
	textBoxUp.setDimensions(cc.size(380, 200));
	textBoxUp.attr({
		anchorX : 0,
		anchorY : 1,
		x: 210,
		y: 170,
		color : cc.color(0, 0, 0)
	});
	showTurn.addChild(textBoxUp, 1000);
	g_GBTouchLayer.addChild(showTurn,900,"showTurn");
	var showThisTurn = cc.Sequence.create(new cc.MoveTo(0.5, cc.p(0, 650)),cc.DelayTime.create(1.3),new cc.MoveTo(0.5, cc.p(-640, 650)),cc.CallFunc.create(function(){GLStartTurn()}));
	showTurn.runAction(showThisTurn);
}
